package com.capgemini.diabeo.nav;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.anupcowkur.wheel_menu.WheelMenu;
import com.capgemini.diabeo.R;
import com.capgemini.diabeo.fragment.DishFragment_;
import com.capgemini.diabeo.manager.DishManager;
import com.capgemini.diabeo.manager.DishManager_;
import com.capgemini.diabeo.manager.MealStatus;
import com.capgemini.diabeo.model.Category;
import com.capgemini.diabeo.nav.model.Sample;
import com.capgemini.diabeo.service.CategoryService_;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EActivity;

/**
 * An activity representing a list of Samples. This activity has different
 * presentations for handset and tablet-size devices. On handsets, the activity
 * presents a list of items, which when touched, lead to a
 * {@link SampleDetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two
 * vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link SampleListFragment} and the item details (if present) is a
 * {@link SampleDetailFragment}.
 * <p>
 * This activity also implements the required
 * {@link SampleListFragment.Callbacks} interface to listen for item selections.
 */
@EActivity(R.layout.activity_sample_list)
public class SampleListActivity extends FragmentActivity implements
SampleListFragment.Callbacks {

	/**
	 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
	 * device.
	 */
	private boolean mTwoPane;

	Sample[] samples;

	@Bean
	DishManager dishManager;

	int selectedElement = 0;

	@AfterViews
	protected void afterViews() {

		ImageView wheel_image = (ImageView) findViewById(R.id.wheel);
		final WheelMenu wm = new WheelMenu(this, 12, R.drawable.wheel, wheel_image, true);
		locateElementsWheel(wheel_image, wm);

		if (findViewById(R.id.sample_detail_container) != null) {
			// The detail container view will be present only in the
			// large-screen layouts (res/values-large and
			// res/values-sw600dp). If this view is present, then the
			// activity should be in two-pane mode.
			mTwoPane = true;

			// In two-pane mode, list items should be given the
			// 'activated' state when touched.
			((SampleListFragment) getSupportFragmentManager().findFragmentById(
					R.id.sample_list)).setActivateOnItemClick(true);
		}

		// TODO: If exposing deep links into your app, handle intents here.
	}

	/**
	 * Callback method from {@link SampleListFragment.Callbacks} indicating that
	 * the item with the given ID was selected.
	 */
	@Override
	public void onItemSelected(Sample sample) {
		dishManager.setCategory(sample.getCategory());
		if (mTwoPane) {
			// In two-pane mode, show the detail view in this activity by
			// adding or replacing the detail fragment using a
			// fragment transaction.
			Fragment fragment = sample.createFragment();
			getSupportFragmentManager().beginTransaction()
			.replace(R.id.sample_detail_container, fragment).commit();

		} else {
			// In single-pane mode, simply start the detail activity
			// for the selected item ID.
			Intent detailIntent = new Intent(this, SampleDetailActivity_.class);
			detailIntent.putExtra(SampleDetailActivity.KEY_SAMPLE, sample);
			startActivity(detailIntent);
		}
	}

	public void locateElementsWheel(ImageView wheel_image, final WheelMenu wm)
	{

		//Size of the screen
		DisplayMetrics metrics=new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		float height=metrics.heightPixels;

		//Size de l'imageview
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.wheel, o);
		int h = bmp.getHeight();

		//Calcul pour placer l'imageview
		float top = (height - (float)h)/2;
		wheel_image.setY(top);
		/* Permet de sélectionner les catégories et leur liste de plat */
		final DishManager_ dishManager = DishManager_.getInstance_(this);
		MealStatus convertedDishStatus = dishManager
				.getConvertedDishStatus();
		CategoryService_ service = CategoryService_.getInstance_(this);
		final List<Category> categories = service
				.retrieveCategoryByMeal(convertedDishStatus);
		samples = new Sample[categories.size()];

		for (int i = 0; i < categories.size(); i++) {
			Sample sample = new Sample(this, categories.get(i).getName(),
					DishFragment_.class, categories.get(i));
			samples[i] = sample;
		}

		/* Bouton OK /!\Pas de gestion tablette pour l'instant*/
		/***!\ Pensez à virer la listfragment derrière /!****/
		Button show_selected = (Button) findViewById(R.id.show_selected_button);
		show_selected.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//use WheelMenu.getSelected() method to get the selected division
				int realCategory = wm.getSelected()-1;
				if(realCategory<categories.size())
				{
					Intent detailIntent = new Intent(getApplicationContext(), SampleDetailActivity_.class);
					dishManager.setCategory(samples[realCategory].getCategory());
					detailIntent.putExtra(SampleDetailActivity.KEY_SAMPLE, samples[realCategory]);
					startActivity(detailIntent);
				}else
				{
					Toast.makeText(getApplicationContext(), "Pas de catégorie " + wm.getSelected() + " trouvée", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
}
